package com.gzw.shop.adapter;

import android.content.Context;
import android.net.Uri;
import android.text.Html;
import android.view.View;
import android.widget.CheckBox;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.gzw.shop.R;
import com.gzw.shop.bean.ShoppingCart;
import com.gzw.shop.utils.CartProvider;
import com.gzw.shop.widget.NumAddSubView;

import java.util.Date;
import java.util.Iterator;
import java.util.List;

import static com.gzw.shop.R.string.cart;

/**
 * Created by JACKYGUO on 2017-03-10.
 */

public class CartAdapter extends SimpleAdapter<ShoppingCart> implements BaseAdapter.OnItemClickListener {

    private CheckBox mCheckBox;
    private TextView mTextView;

    private List<ShoppingCart> datas;

    private CartProvider mProvider;

    public CartAdapter(Context context, final List<ShoppingCart> datas, CheckBox checkBox, TextView textView) {
        super(context, datas, R.layout.template_cart);
        mCheckBox = checkBox;
        mTextView = textView;
        this.datas = datas;
        mProvider = new CartProvider(context);

        mCheckBox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                boolean isCheck = mCheckBox.isChecked();
                checkAll(isCheck);
                showTotalPrice();
            }
        });

        setListener(this);
        showTotalPrice();
    }


    @Override
    public void bindData(BaseViewHolder holder, final ShoppingCart cart) {

        holder.getTextView(R.id.text_title).setText(cart.getName());
        holder.getTextView(R.id.text_price).setText("￥" + cart.getPrice());

        SimpleDraweeView draweeView = (SimpleDraweeView) holder.getView(R.id.drawee_view);
        draweeView.setImageURI(Uri.parse(cart.getImgUrl()));

        CheckBox checkBox = (CheckBox) holder.getView(R.id.checkbox);
        checkBox.setChecked(cart.isChecked());

        NumAddSubView numAddSubView = (NumAddSubView) holder.getView(R.id.num_control);
        numAddSubView.setValue(cart.getCount());

        numAddSubView.setListener(new NumAddSubView.OnButtonClickListener() {
            @Override
            public void onButtonAddClick(View view, int value) {
                cart.setCount(value);
                mProvider.update(cart);
                showTotalPrice();
            }

            @Override
            public void onButtonSubClick(View view, int value) {

                cart.setCount(value);
                mProvider.update(cart);
                showTotalPrice();
            }
        });
    }

    public void checkAll(boolean isCheck) {

        if (!isNull()) {
            showTotalPrice();
            return;
        }

        int i = 0;
        for (ShoppingCart cart : datas) {
            cart.setChecked(isCheck);
            notifyItemChanged(i);
            i++;
        }
        showTotalPrice();
    }

    //更改价格
    public void showTotalPrice() {
        float total = getTotalPrice();
        mTextView.setText(Html.fromHtml("合计 ￥ <span style='color:#eb4f38'>" + total + "<span>"), TextView.BufferType.SPANNABLE);
    }

    private float getTotalPrice() {
        float sum = 0;
        if (!isNull()) {
            return sum;
        }

        for (ShoppingCart cart : datas) {
            if (cart.isChecked()) {
                sum += cart.getCount() * cart.getPrice();
            }
        }
        return sum;
    }

    private boolean isNull() {
        return (datas != null && datas.size() > 0);
    }

    @Override
    public void OnClick(View view, int position) {

        ShoppingCart cart = getItem(position);
        cart.setChecked(!cart.isChecked());
        notifyItemChanged(position);
        checkListen();
        showTotalPrice();
    }

    //设置全与非全选
    private void checkListen() {
        int count = 0;
        int checkNum = 0;
        if (datas != null) {
            count = datas.size();

            for (ShoppingCart cart : datas) {
                if (!cart.isChecked()) {
                    mCheckBox.setChecked(false);
                    break;
                } else {
                    checkNum = checkNum + 1;
                }
            }
            if (count == checkNum) {
                mCheckBox.setChecked(true);
            }
        }
    }

    //删除购物车
    public void delCart() {

        if (!isNull()) {
            return;
        }

        for (Iterator iterator = datas.iterator(); iterator.hasNext(); ) {
            ShoppingCart cart = (ShoppingCart) iterator.next();
            if (cart.isChecked()) {
                int position = datas.indexOf(cart);
                mProvider.delete(cart);
                iterator.remove();
                notifyItemRemoved(position);
            }
        }
    }
}
